/**
 * @(#)AuthTask.java 1.0 2013/9/4
 * Copyright 2013 PD, Inc. All rights reserved.
 */
package cn.broadin.app.hotel.task;

import android.os.AsyncTask;
import android.util.Log;
import cn.broadin.app.hotel.auth.Auth;
import cn.broadin.app.hotel.factory.ApplicationFactory;
import cn.broadin.app.hotel.startWebapp.LoadActivity;
import cn.broadin.app.hotel.util.PDUtils;

import com.alibaba.fastjson.JSON;

/**
 * AuthTask 鉴权task
 * @author fenghui
 *
 */
public class AuthTask extends AsyncTask<Void, Void, Void>{

	private static final String TAG = "AuthTask";			//log tag
	private LoadActivity loadActivity;						//处理完后调用该类中方法
	private String httpResult = "";							//http请求结果
	
	public AuthTask(LoadActivity activity) {
		loadActivity = activity;
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}

	@Override
	protected Void doInBackground(Void... params) {
		//请求鉴权信息
		String authUrl = ApplicationFactory.getApplication().AUTHURL() + PDUtils.getInstance().getMacAddress(loadActivity, true);
		httpResult = PDUtils.getInstance().getHttpRequest(authUrl);
		Log.d(TAG, "getResut:>> " + httpResult);
		return null;
	}
	
	@Override
	protected void onPostExecute(Void result) {
		Auth auth = null;
		try {
			auth = (Auth)JSON.parseObject(httpResult, Auth.class);
			if(auth.getDataArea().get(0).getResult() == 0) {
				loadActivity.toMainPage();
			} else {
				loadActivity.showEeeorMsg("无访问权限！");
			}
		} catch (Exception e) {
			Log.e(TAG, "get auth data error." + e.getMessage());
			loadActivity.showEeeorMsg("无访问权限！");
		}
	}
}